package com.hpin.fansea.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.bean.MyRepairs;
import com.hpin.fansea.my.MyRepairsDetailActivity;
import com.hpin.fansea.utils.AbDateUtil;

import java.util.List;

/**
 * 我的-保修记录的适配器
 *
 * @author pan
 */
public class MyRepairsAdapter extends BaseAdapter {
    private List<MyRepairs.RepairDetail> repairDetailList;
    private Context context;
//	private Boolean isDelete = false;
//	// 缓存checkbox的状态
//	private static HashMap<Integer, Boolean> isSelected;

    public MyRepairsAdapter(List<MyRepairs.RepairDetail> list, Context context) {
        super();
        this.repairDetailList = list;
        this.context = context;

//		isSelected = new HashMap<Integer, Boolean>();

//		for (int i = 0; i < list.size(); i++) {
//			getIsSelected().put(i, false);
//		}
    }

    public void setRepairDetailList(List<MyRepairs.RepairDetail> repairDetailList) {
        this.repairDetailList = repairDetailList;
    }

    //	public void setDelete(Boolean isDelete) {
//		this.isDelete = isDelete;
//	}

    @Override
    public int getCount() {
        return repairDetailList.size();
    }

    @Override
    public Object getItem(int position) {
        return repairDetailList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_my_repairs, null);
            holder = new ViewHolder();
            holder.repairs_name = (TextView) convertView.findViewById(R.id.my_repairs_name);
            holder.repairs_time = (TextView) convertView.findViewById(R.id.my_repairs_time);
            holder.repairs_state = (TextView) convertView.findViewById(R.id.my_repairs_state);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.repairs_name.setText(repairDetailList.get(position).maintenanceCont);
//		String time = ;
        holder.repairs_time.setText(AbDateUtil.formatDateStr2Desc(repairDetailList.get(position).createTime, "MM月dd日"));

        switch (repairDetailList.get(position).maintenanceStatus) {

            case "1":
                holder.repairs_state.setText("正在处理中");
                break;

            case "2":
                holder.repairs_state.setText("待处理");
                break;

            case "3":
                holder.repairs_state.setText("处理完成");
                break;
            default:
                holder.repairs_state.setText("未知状态");
                break;
        }

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, MyRepairsDetailActivity.class);
                Bundle bundle = new Bundle();
                MyRepairs.RepairDetail repairDetail = repairDetailList.get(position);
                bundle.putSerializable("repairDetailList", repairDetail);
                intent.putExtras(bundle);
                context.startActivity(intent);

            }
        });

        return convertView;
    }

   private class ViewHolder {
        private TextView repairs_name, repairs_time, repairs_state;
//        private CheckBox my_repairs_checkbox;
    }
}
